package com.cdzy.controller;

import com.cdzy.pojo.Good;
import com.cdzy.pojo.GoodOrder;
import com.cdzy.pojo.Lecture;
import com.cdzy.pojo.Student;
import com.cdzy.service.LoginService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/Good")
public class good {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/uploadGood")
    public String goods(int integral, String name) throws JsonProcessingException {
        loginService.updateGood(integral, name);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString("修改成功");
    };

    @RequestMapping("/queryUserIntegral")
    public String queryUserIntegral(String name) throws JsonProcessingException {
        ArrayList<Student> lecture2 = loginService.queryUserIntegral(name);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(lecture2);
    };

    @RequestMapping("/queryGood")
    public String queryGood() throws JsonProcessingException {
        ArrayList<Good> lecture2 = loginService.queryGood();
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(lecture2);
    };

    @RequestMapping("/updateGoodOrder")
    public String updateGoodOrder(String id, String exchange) throws JsonProcessingException {
        loginService.updateGoodOrder(id, exchange);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString("兑换成功!");
    };

    @RequestMapping("/addGoodOrder")
    public String addGoodOrder(String good_name, String name, String exchange) throws JsonProcessingException {
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0,8);
        loginService.addGoodOrder(uuid, good_name, name, exchange);
        return "成功";
    };

    @RequestMapping("/queryGoodOrder")
    public String queryGoodOrder(String id) throws JsonProcessingException {
        ArrayList<GoodOrder> lecture = loginService.queryGoodOrder(id);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(lecture);
    };

    @RequestMapping("/queryGoodOrderAll")
    public String queryGoodOrderAll() throws JsonProcessingException {
        ArrayList<GoodOrder> lecture = loginService.queryGoodOrderAll();
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(lecture);
    };

    @RequestMapping("/deleteGood")
    public String deleteGood(String good_name) throws JsonProcessingException {
        loginService.deleteGood(good_name);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString("删除成功");
    };
}
